{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2023 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# simple_max_flow_program"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/graph/simple_max_flow_program.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/ortools/graph/samples/simple_max_flow_program.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "From Taha 'Introduction to Operations Research', example 6.4-2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "from ortools.graph.python import max_flow\n",
    "\n",
    "\n",
    "def main():\n",
    "    \"\"\"MaxFlow simple interface example.\"\"\"\n",
    "    # Instantiate a SimpleMaxFlow solver.\n",
    "    smf = max_flow.SimpleMaxFlow()\n",
    "\n",
    "    # Define three parallel arrays: start_nodes, end_nodes, and the capacities\n",
    "    # between each pair. For instance, the arc from node 0 to node 1 has a\n",
    "    # capacity of 20.\n",
    "    start_nodes = np.array([0, 0, 0, 1, 1, 2, 2, 3, 3])\n",
    "    end_nodes = np.array([1, 2, 3, 2, 4, 3, 4, 2, 4])\n",
    "    capacities = np.array([20, 30, 10, 40, 30, 10, 20, 5, 20])\n",
    "\n",
    "    # Add arcs in bulk.\n",
    "    #   note: we could have used add_arc_with_capacity(start, end, capacity)\n",
    "    all_arcs = smf.add_arcs_with_capacity(start_nodes, end_nodes, capacities)\n",
    "\n",
    "    # Find the maximum flow between node 0 and node 4.\n",
    "    status = smf.solve(0, 4)\n",
    "\n",
    "    if status != smf.OPTIMAL:\n",
    "        print(\"There was an issue with the max flow input.\")\n",
    "        print(f\"Status: {status}\")\n",
    "        exit(1)\n",
    "    print(\"Max flow:\", smf.optimal_flow())\n",
    "    print(\"\")\n",
    "    print(\" Arc    Flow / Capacity\")\n",
    "    solution_flows = smf.flows(all_arcs)\n",
    "    for arc, flow, capacity in zip(all_arcs, solution_flows, capacities):\n",
    "        print(f\"{smf.tail(arc)} / {smf.head(arc)}   {flow:3}  / {capacity:3}\")\n",
    "    print(\"Source side min-cut:\", smf.get_source_side_min_cut())\n",
    "    print(\"Sink side min-cut:\", smf.get_sink_side_min_cut())\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
